/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.order.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ocm.core.tool.utils.DateUtil;
import com.ocm.order.dto.*;
import com.ocm.order.entity.Order;
import com.ocm.order.service.IOrderService;
import com.ocm.order.vo.OrderVO;
import com.ocm.order.vo.UOrderDetailVO;
import com.ocm.order.vo.UOrderVO;
import com.ocm.order.wrapper.OrderWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.common.constant.OrderStatusConstant;
import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.secure.BladeUser;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.utils.BeanUtil;
import com.ocm.core.tool.utils.Func;
import com.ocm.order.dto.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.core.boot.ctrl.BladeController;

/**
 *  控制器
 *
 * @author HQY
 * @since 2022-01-16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/order")
@Api(value = "", tags = "接口")
public class OrderController extends BladeController {

	private final IOrderService orderService;


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入order")
	public R<OrderVO> detail(Order order) {
		Order detail = orderService.getOne(Condition.getQueryWrapper(order));
		return R.data(OrderWrapper.build().entityVO(detail));
	}

	@GetMapping("/u/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "订单详情", notes = "传入order")
	public R<UOrderDetailVO> uDetail(Order order) {
		return R.data(orderService.getOrderDetail(order));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入order")
	public R<IPage<OrderVO>> list(Order order, Query query) {
		IPage<Order> pages = orderService.page(Condition.getPage(query), Condition.getQueryWrapper(order));
		return R.data(OrderWrapper.build().pageVO(pages));
	}

	@GetMapping("/u/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入order")
	public R uList(Integer status, Query query, BladeUser bladeUser) {
		IPage<UOrderVO> pages = orderService.uList(Condition.getPage(query),bladeUser.getUserId(),status);
		return R.data(pages);
	}


	@PostMapping("/u/cancel")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "订单未支付-取消", notes = "传入order")
	public R uCancel(@RequestBody Long orderId, BladeUser bladeUser) {
		Order order = new Order();
		order.setUpdateTime(DateUtil.now());
		order.setStatus(OrderStatusConstant.ORDER_STATUS_SHUTDOWN);
		return R.status(orderService.update(order,new UpdateWrapper<Order>()
			.eq("id",orderId).eq("status",OrderStatusConstant.ORDER_STATUS_UNPAID)
			.eq("create_user",bladeUser.getUserId())
			.eq("is_deleted",0)));
	}

	@PostMapping("/u/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "订单未发货-更新地址", notes = "传入order")
	public R uCancel(@RequestBody OrderAddressDTO orderAddressDTO, BladeUser bladeUser) {
		Order o = BeanUtil.copy(orderAddressDTO, Order.class);
		return R.status(orderService.update(o,new UpdateWrapper<Order>()
			.eq("id",orderAddressDTO.getOrderId())
			.eq("is_deleted",0)
			.eq("create_user",bladeUser.getUserId())
			.in("status",OrderStatusConstant.ORDER_STATUS_PAID,OrderStatusConstant.ORDER_STATUS_UNPAID)));
	}


	@PostMapping("/u/pay")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "订单支付", notes = "传入order")
	public R uPay(@Valid @RequestBody OrderPayDTO orders, BladeUser bladeUser) {
		return R.status(orderService.uPay(orders,bladeUser.getUserId(),bladeUser.getUserName()));
	}

	@PostMapping("u/refund")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "订单退款/未发货", notes = "传入order")
	@Transactional
	public R uRefund(@Valid @RequestBody RefundDTO orders, BladeUser bladeUser) {
		orderService.refund(orders.getOrderId(),bladeUser.getUserId());
		return R.success("操作成功");
	}


	@PostMapping("u/receiving")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "订单收货", notes = "传入order")
	@Transactional
	public R uReceiving(@RequestBody Long orderId, BladeUser bladeUser) {

		orderService.receiving(orderId,bladeUser.getUserId());

		return R.success("操作成功");
	}

	@PostMapping("/u/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R uRemove(@ApiParam(value = "主键集合", required = true)@RequestBody Long id) {
		return R.status(orderService.update(new UpdateWrapper<Order>()
			.set("is_deleted",1)
			.eq("id",id)
			.eq("status",OrderStatusConstant.ORDER_STATUS_SHUTDOWN)));
	}


	@PostMapping("/s/deliver")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "订单发货", notes = "传入deliverDTO")
	@Transactional
	public R sDeliver(@Valid @RequestBody DeliverDTO deliverDTO, BladeUser bladeUser) {
		Long shopId = bladeUser.getShopId();
		orderService.deliver(deliverDTO,shopId);
		return R.success("操作成功");
	}

	@PostMapping("/u/create")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入order")
	public R uCreate(@Valid @RequestBody OrderDTO order) {
		return R.data(orderService.create(order));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入order")
	public R<IPage<OrderVO>> page(OrderVO order, Query query) {
		IPage<OrderVO> pages = orderService.selectOrderPage(Condition.getPage(query), order);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入order")
	public R save(@Valid @RequestBody Order order) {
		return R.status(orderService.save(order));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入order")
	public R update(@Valid @RequestBody Order order) {
		return R.status(orderService.updateById(order));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入order")
	public R submit(@Valid @RequestBody Order order) {
		return R.status(orderService.saveOrUpdate(order));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
//		List<Long> longs = Func.toLongList(ids);
//		List<Order> orders = orderService.listByIds(longs);
//
		return R.status(orderService.deleteLogic(Func.toLongList(ids)));
	}


}
